/* Eeast rogue: Chinese roguelike game. */
/* version: 0.1 2012-05-15  */
/* copyright (c) Jiqing Wu 2012-05-15. */
/* email: jiqingwu@gmail.com  */

#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>

#include "text.h"
#include "config.h"
#include "sdl_func.h"
#include "role.h"
#include "util.h"
#include "effect.h"
#include "map.h"
#include "file.h"
#include "ai.h"

extern SDL_Surface * screen;
extern SDL_Surface * tiles;


int
main(argc, argv)
    int argc;
    char ** argv;
{

    InitVideo();

    OpenTtfFonts();
    InitAudio();

    MapGenerate();
    ClipRect r;
    r.startX = 0; r.endX = MAP_COLS -1;
    r.startY = 0; r.endY = MAP_ROWS -1;
    MapDraw(r);

    Role u;
    RoleInit(&u, 346, 12, 10);
    RoleDraw(&u);

    SDL_Flip(screen);

    boolean running = true;
    SDL_Event event;
    Effect e;

    while (running){
        SDL_WaitEvent(&event);
        switch (event.type) {
        case SDL_KEYDOWN:
            switch (event.key.keysym.sym) {
            case SDLK_h:
            case SDLK_LEFT:
                RoleMove(&u, dir_left);
                break;
            case SDLK_j:
            case SDLK_DOWN:
                RoleMove(&u, dir_down);
                break;
            case SDLK_k:
            case SDLK_UP:
                RoleMove(&u, dir_up);
                break;
            case SDLK_l:
            case SDLK_RIGHT:
                RoleMove(&u, dir_right);
                break;
            case SDLK_y:
                RoleMove(&u, dir_upleft);
                break;
            case SDLK_u:
                RoleMove(&u, dir_upright);
                break;
            case SDLK_b:
                RoleMove(&u, dir_downleft);
                break;
            case SDLK_n:
                RoleMove(&u, dir_downright);
                break;
            case SDLK_c:
                printf("cast spell ...\n");
                RoleCastSpell(&u, u.dir);
                break;
            case SDLK_s:
                if (KMOD_CAPS || KMOD_SHIFT)
                    running = false;
            }
            break;
        case SDL_MOUSEBUTTONDOWN:
        {
            int mouse_x, mouse_y, grid_x, grid_y;
            int from, to;
            mouse_x = event.button.x;
            mouse_y = event.button.y;
            grid_x = (mouse_x - MAP_OFFSETX) / TILE_WIDTH;
            grid_y = (mouse_y - MAP_OFFSETY) / TILE_HEIGHT;
            from = u.pos.x + u.pos.y * MAP_COLS;
            to = grid_x + grid_y * MAP_COLS;

            if(u.dest_grid == to) {
                while(RoleHasPath(&u)){
                    RoleFollowPath(&u);
                    SDL_Delay(100);
                }
            }
            else {
                if(AstarFindPath(from, to, war_map, u.path)){
                    u.dest_grid = to;
                    ClipRect r = {0, MAP_COLS - 1, 0, MAP_ROWS - 1};
                    MapDraw(r);
                    DrawPath(u.path, from);
                    RoleDraw(&u);
                    SDL_Flip(screen);
                }
            }
            break;
        }
        case SDL_QUIT:
            running = false;
        }
    }

    CloseTtfFonts();
    ShutdownVideo();
    ShutdownAudio();
    return 0;
}
